package com.rgs.gateway.common.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.rgs.gateway.common.enums.SystemErrorEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;

@ApiModel(description = "rest请求的返回模型，所有rest正常都返回该类的对象")
@Getter
public class Result<T> {

    public static final Integer FAIL_CODE = 500;
    public static final Integer SUCCESSFUL_CODE = 200;
    public static final String SUCCESSFUL_MESG = "处理成功";

    @ApiModelProperty(value = "处理结果code", required = true)
    private Integer code;
    @ApiModelProperty(value = "处理结果描述信息")
    private String message;
    @ApiModelProperty(value = "处理结果数据信息")
    private T data;

    public Result() {
    }


    /**
     * @param code
     * @param message
     */
    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 内部使用，用于构造成功的结果
     *
     * @param code
     * @param message
     * @param data
     */
    private Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 快速创建成功结果并返回结果数据
     *
     * @param data
     * @return Result
     */
    public static Result success(Object data) {
        return new Result<>(SUCCESSFUL_CODE, SUCCESSFUL_MESG, data);
    }

    /**
     * 快速创建成功结果
     *
     * @return Result
     */
    public static Result success() {
        return success(null);
    }

    /**
     * 系统异常类没有返回数据
     *
     * @return Result
     */
    public static Result fail(Integer code, String message) {
        return new Result(code, message);
    }
    /**
     * 系统异常类没有返回数据
     *
     * @return Result
     */
    public static Result fail(String message) {
        return new Result(FAIL_CODE, message);
    }

    /**
     * 系统异常类没有返回数据
     *
     * @return Result
     */
    public static Result fail() {
        return new Result(500,"系统异常");
    }


    /**
     * 系统异常类没有返回数据
     *
     * @return Result
     */
    public static Result fail(SystemErrorEnum systemErrorEnum) {
        return new Result(systemErrorEnum.getCode(),systemErrorEnum.getMessage());
    }



    /**
     * 成功code=000000
     *
     * @return true/false
     */
    @JsonIgnore
    public boolean isSuccess() {
        return SUCCESSFUL_CODE.equals(this.code);
    }

    /**
     * 失败
     *
     * @return true/false
     */
    @JsonIgnore
    public boolean isFail() {
        return !isSuccess();
    }

    /**
     * 日志链路id
     */
    private String traceId;

    public void setTraceId(String traceId){
        this.traceId = traceId;
    }
}
